package com.vichat.utils;

import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.vichat.online.entity.StatOnlineUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class OnlineUtil {

	private static transient final Logger logger = LoggerFactory.getLogger(OnlineUtil.class);




	public static HashMap<Long, StatOnlineUser> onlineImUser = new HashMap<Long, StatOnlineUser>();

	public static Object userlock = new Object();


	public static SimplePropertyPreFilter filter = new SimplePropertyPreFilter();

	static {
		filter.getExcludes().add("uid");
		filter.getExcludes().add("partyId");
		filter.getExcludes().add("uid");
		filter.getExcludes().add("state");
		filter.getExcludes().add("outTime");
	}


	/**
	 * 处理online_user，opflag： 1 登录 0 继续在线 -1 登出
	 *
	 * @param onlineUser
	 * @param opflag
	 */
	public static void changeOnlineUser(StatOnlineUser onlineUser, int opflag) {

		synchronized (userlock) {
			if (opflag == 1) {
				onlineImUser.put(onlineUser.getUid(), onlineUser);
				onlineUser.setImDisplayState(onlineUser.getImDisplayState());
//				String userInfo = JSON.toJSONString(onlineUser, filter);
//				RedisHelper.setPartyUserOnline(onlineUser.getPartyId(), onlineUser.getUid(), userInfo);
//				logger.debug("登录:" + userInfo);
			}
			if (opflag == 0) {
				onlineImUser.put(onlineUser.getUid(), onlineUser);
				onlineUser.setImDisplayState(onlineUser.getImDisplayState());
//				String userInfo = JSON.toJSONString(onlineUser, filter);
//				RedisHelper.setPartyUserOnline(onlineUser.getPartyId(), onlineUser.getUid(), userInfo);
//				logger.debug("在线:" + userInfo);
			}
			if (opflag == -1) {
				onlineImUser.remove(onlineUser.getUid());
//				RedisHelper.removePartyUserOnline(onlineUser.getPartyId(), onlineUser.getUid());
//				logger.debug("登出:" + onlineUser);
			}

		}
	}

	public static ExecutorService userExec0 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec1 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec2 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec3 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec4 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec5 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec6 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec7 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec8 = Executors.newFixedThreadPool(10);
	public static ExecutorService userExec9 = Executors.newFixedThreadPool(10);

	public static void startUserStateDealer(Thread userdeal, long mod){

		if(mod==0){
			userExec0.execute(userdeal);
		}else if(mod==1){
			userExec1.execute(userdeal);
		}else if(mod==2){
			userExec2.execute(userdeal);
		}else if(mod==3){
			userExec3.execute(userdeal);
		}else if(mod==4){
			userExec4.execute(userdeal);
		}else if(mod==5){
			userExec5.execute(userdeal);
		}else if(mod==6){
			userExec6.execute(userdeal);
		}else if(mod==7){
			userExec7.execute(userdeal);
		}else if(mod==8){
			userExec8.execute(userdeal);
		}else {
			userExec9.execute(userdeal);
		}
	}
}
